package com.example.demo.controller;

import com.example.demo.model.Restaurant;
import com.example.demo.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class RestaurantController {

    private final RestaurantService restaurantService;

    @Autowired
    public RestaurantController(RestaurantService restaurantService) {
        this.restaurantService = restaurantService;
    }

    @GetMapping("/restaurants")
    public List<Restaurant> getRestaurants(
            @RequestParam(value = "name", required = false) String name) {
        List<Restaurant> restaurants = restaurantService.getAllRestaurants();
        if (name != null && !name.isEmpty()) {
            return restaurants.stream()
                    .filter(restaurant -> restaurant.getName().contains(name))
                    .collect(Collectors.toList());
        }
        return restaurants;
    }

    // 使用 @RequestBody 添加新餐厅
    @PostMapping("/restaurants")
    public Restaurant addRestaurant(@RequestBody Restaurant restaurant) {
        restaurantService.addRestaurant(restaurant);
        return restaurant; // 返回新增的餐厅
    }
}
